% File: sc_fde_preprocess.m
% -------------------------
% This script maintains system profile and evaluation settings

% channel settings
BW          = 20e6;       % bandwidth in bits/sec.
Ts          = 1/BW;       % symbol interval
doppler_max = 0;          % max doppler shift in Hz
chan_mode   = 0;          % channel mode. 0(default): rayleighchan; 1: ricianchan; 2: stdchan
switch chan_profile
    case 1
        path_delays_norm = (0:27);
        h_test_rayleigh  = [ -0.0548 - 0.0004i   0.0660 - 0.0002i  -0.0840 + 0.0015i   0.1195 - 0.0050i  -0.2550 - 0.0121i  -0.4627 - 0.3272i  -0.1349 - 0.0126i   0.0927 - 0.1496i 0.1652 - 0.0911i   0.1703 - 0.0371i   0.3069 + 0.0051i  -0.1531 + 0.0293i   0.0903 - 0.0165i  -0.0672 - 0.0169i   0.0539 - 0.1156i  -0.0482 + 0.0004i 0.0415 + 0.0474i  -0.0370 + 0.0101i   0.0307 - 0.0376i  -0.0363 - 0.0430i   0.0314 + 0.0158i  -0.0251 - 0.0139i   0.0289 + 0.0082i  -0.0351 - 0.0087i  0.0100 + 0.0022i  -0.0166 - 0.0034i   0.0170 + 0.0032i  -0.0167 - 0.0030i ];
        pdp              = 20*log10(abs(h_test_rayleigh)); % severe-ISI, equally decaying Rayleigh fading channel
        chan_type = 'hiperlan2E';
    case 2                % exponentially decaying with ds_rms = 10*Ts
        path_delays_norm = (0:63); % multipath delay normalized to Ts
        ds_rms = 20;     % root mean square delay spread normalized to Ts
        pdp = exp(-1*path_delays_norm/ds_rms);
        pdp = pdp/(sum(pdp));
        pdp = 10*log10(pdp);
    case 3                % exponentially decaying with ds_rms = 10*Ts
        path_delays_norm = (0:97); % multipath delay normalized to Ts
        ds_rms = 20;     % root mean square delay spread normalized to Ts
        pdp = exp(-1*path_delays_norm/ds_rms);
        pdp = pdp/(sum(pdp));
        pdp = 10*log10(pdp);
    case 4
        BW        = 50e6;       % bandwidth in bits/sec.
        Ts        = 1/BW;       % symbol interval
        chan_mode = 3; % std_chan
        chan_type = 'hiperlan2C';
        path_delays_norm     = (0:63); % multipath delay normalized to Ts, test needed for stdchan, refer to "test_channel_ir.m" for detail
        [std_chan,std_chan_profile] = stdchan(Ts,doppler_max,chan_type);
        pdp       = std_chan_profile.AvgPathGaindB;
        ds        = std_chan_profile.PathDelays;
        pdp_ls    = 10.^(pdp/10);
        ds_mean   = sum(ds.*pdp_ls)/sum(pdp_ls);
        ds_rms    = sqrt(sum((ds-ds_mean).^2.*pdp_ls)/sum(pdp_ls));
    case 5
        BW        = 50e6;       % bandwidth in bits/sec.
        Ts        = 1/BW;       % symbol interval
        chan_mode = 3; % std_chan
        chan_type = 'hiperlan2E';
        path_delays_norm     = (0:96); % multipath delay normalized to Ts, test needed for stdchan, refer to "test_channel_ir.m" for detail
        [std_chan,std_chan_profile] = stdchan(Ts,doppler_max,chan_type);
        pdp       = std_chan_profile.AvgPathGaindB;
        ds        = std_chan_profile.PathDelays;
        pdp_ls    = 10.^(pdp/10);
        ds_mean   = sum(ds.*pdp_ls)/sum(pdp_ls);
        ds_rms    = sqrt(sum((ds-ds_mean).^2.*pdp_ls)/sum(pdp_ls));
    case 6
        path_delays_norm = (0:63);
        pdp              = zeros(1,length(path_delays_norm)); % severe-ISI, equally decaying Rayleigh fading channel
    case 7
        BW        = 50e6;       % bandwidth in bits/sec.
        Ts        = 1/BW;       % symbol interval
        chan_mode = 3;          % std_chan
        chan_type = 'hiperlan2A';
        path_delays_norm     = (0:27); % multipath delay normalized to Ts, test needed for stdchan, refer to "test_channel_ir.m" for detail
        [std_chan,std_chan_profile] = stdchan(Ts,doppler_max,chan_type);
        pdp       = std_chan_profile.AvgPathGaindB;
        ds        = std_chan_profile.PathDelays;
        pdp_ls    = 10.^(pdp/10);
        ds_mean   = sum(ds.*pdp_ls)/sum(pdp_ls);
        ds_rms    = sqrt(sum((ds-ds_mean).^2.*pdp_ls)/sum(pdp_ls));
    otherwise
        path_delays_norm = (0:7);
        pdp              = zeros(1,length(path_delays_norm)); % severe-ISI, equally decaying Rayleigh fading channel
end
length_chan = max(path_delays_norm)+1;

% multiuser settings
switch mu_profile
    case 1
        ...
    otherwise
        nu = 1;
        nt = nu;
        nr = 1;
end

% signal and packet settings
switch sig_pac_profile
    case 1
        length_cp = 16;    % short cp
        nsymb = 256;      % # symbols per frame
        sf    = 1;        % spreading factor
        nfft  = sf*nsymb; % fft size
        M_mod = 4;        % Alphabet size
        nsubf = 4;        % # frames in a superframe
    case 2
        length_cp = 8;    % no cp
        nsymb = 256;      % # symbols per frame
        sf    = 1;        % spreading factor
        nfft  = sf*nsymb; % fft size
        M_mod = 4;        % Alphabet size
        nsubf = 4;        % # frames in a superframe
    case 3
        length_cp = 8;    % short cp
        nsymb = 256;      % # symbols per frame
        sf    = 1;        % spreading factor
        nfft  = sf*nsymb; % fft size
        M_mod = 4;        % Alphabet size
        nsubf = 1;        % # frames in a superframe
    case 4
        length_cp = 0;    % no cp
        nsymb = 256;      % # symbols per frame
        sf    = 1;        % spreading factor
        nfft  = sf*nsymb; % fft size
        M_mod = 4;        % Alphabet size
        nsubf = 4;        % # frames in a superframe
    case 5
        length_cp = 16;   % short cp
        nsymb = 256;      % # symbols per frame
        sf    = 1;        % spreading factor
        nfft  = sf*nsymb; % fft size
        M_mod = 4;        % Alphabet size
        nsubf = 4;        % # frames in a superframe
    case 6
        length_cp = 24;   % short cp
        nsymb = 256;      % # symbols per frame
        sf    = 1;        % spreading factor
        nfft  = sf*nsymb; % fft size
        M_mod = 4;        % Alphabet size
        nsubf = 4;        % # frames in a superframe
    case 7
        length_cp = 32;   % short cp
        nsymb = 256;      % # symbols per frame
        sf    = 1;        % spreading factor
        nfft  = sf*nsymb; % fft size
        M_mod = 4;        % Alphabet size
        nsubf = 4;        % # frames in a superframe
    case 8
        length_cp = 40;   % short cp
        nsymb = 256;      % # symbols per frame
        sf    = 1;        % spreading factor
        nfft  = sf*nsymb; % fft size
        M_mod = 4;        % Alphabet size
        nsubf = 4;        % # frames in a superframe
    case 9
        length_cp = 48;   % short cp
        nsymb = 256;      % # symbols per frame
        sf    = 1;        % spreading factor
        nfft  = sf*nsymb; % fft size
        M_mod = 4;        % Alphabet size
        nsubf = 4;        % # frames in a superframe
    case 10
        length_cp = 56;   % short cp
        nsymb = 256;      % # symbols per frame
        sf    = 1;        % spreading factor
        nfft  = sf*nsymb; % fft size
        M_mod = 4;        % Alphabet size
        nsubf = 4;        % # frames in a superframe
    case 11
        length_cp = 64;   % short cp
        nsymb = 256;      % # symbols per frame
        sf    = 1;        % spreading factor
        nfft  = sf*nsymb; % fft size
        M_mod = 4;        % Alphabet size
        nsubf = 4;        % # frames in a superframe
    case 12
        length_cp = 72;   % short cp
        nsymb = 256;      % # symbols per frame
        sf    = 1;        % spreading factor
        nfft  = sf*nsymb; % fft size
        M_mod = 4;        % Alphabet size
        nsubf = 4;        % # frames in a superframe
    case 13
        length_cp = length_chan;   % full length cp
        nsymb = 256;      % # symbols per frame
        sf    = 1;        % spreading factor
        nfft  = sf*nsymb; % fft size
        M_mod = 4;        % Alphabet size
        nsubf = 4;        % # frames in a superframe
    case 14 
        length_cp = length_chan; % full length cp
        nsymb = 2048;      % # symbols per frame
        sf    = 1;        % spreading factor
        nfft  = sf*nsymb; % fft size
        M_mod = 4;        % Alphabet size
        nsubf = 1;        % # frames in a superframe
    case 15
        length_cp = length_chan; % full length cp
        nsymb = 2048;      % # symbols per frame
        sf    = 1;        % spreading factor
        nfft  = sf*nsymb; % fft size
        M_mod = 8;        % Alphabet size
        nsubf = 1;        % # frames in a superframe
    case 16
        length_cp = length_chan; % full length cp
        nsymb = 2048;      % # symbols per frame
        sf    = 1;        % spreading factor
        nfft  = sf*nsymb; % fft size
        M_mod = 16;        % Alphabet size
        nsubf = 1;        % # frames in a superframe
    case 17
        length_cp = length_chan; % full length cp
        nsymb = 2048;      % # symbols per frame
        sf    = 1;        % spreading factor
        nfft  = sf*nsymb; % fft size
        M_mod = 32;        % Alphabet size
        nsubf = 1;        % # frames in a superframe
    case 18
        length_cp = length_chan; % full length cp
        nsymb = 2048;      % # symbols per frame
        sf    = 1;        % spreading factor
        nfft  = sf*nsymb; % fft size
        M_mod = 64;        % Alphabet size
        nsubf = 1;        % # frames in a superframe
    otherwise 
        length_cp = length_chan; % full length cp
        nsymb = 1024;      % # symbols per frame
        sf    = 1;        % spreading factor
        nfft  = sf*nsymb; % fft size
        M_mod = 4;        % Alphabet size
        nsubf = 1;        % # frames in a superframe
end
Cr    = nsymb/(nsymb+length_cp); % code rate
length_del = length_chan - length_cp;
tranc = diag([ones(1,length_del) zeros(1,nsymb-length_del)]);
bit_sym    = log2(M_mod);

% code setting
switch code_profile
    case 1
        crate     = 1/2;       % code rate
        cnst_leng = 7;         % constraint length
        gen_poly  = [133 175]; % generator polynomials
        tbleng    = 35;        % track back length
        decdel    = 35;        % decoding delay
        trel      = poly2trellis(cnst_leng, gen_poly); % Trellis
    case 2
        crate     = 1/2;       % code rate
        cnst_leng = 5;         % constraint length
        gen_poly  = [31 33]; % generator polynomials
        tbleng    = 25;        % track back length
        decdel    = 25;        % decoding delay
        trel      = poly2trellis(cnst_leng, gen_poly); % Trellis
    case 3
        crate     = 1/2;       % code rate
        cnst_leng = 3;         % constraint length
        gen_poly  = [7 5]; % generator polynomials
        tbleng    = 15;        % track back length
        decdel    = 15;        % decoding delay
        trel      = poly2trellis(cnst_leng, gen_poly); % Trellis
    otherwise % no coding
        crate     = 1;
        decdel    = 0;
end
nbit  = bit_sym*nsymb*crate;

% simulation settings
switch sim_profile
    case 1
        numTrials = 50000;   % #of frames in each evaluation
        Eb_N0     = 18:2:20; % Eb/N0, in dB
    case 2
        numTrials = 5000; % #of frames in each evaluation
        Eb_N0     = 14:2:16;
    case 3
        numTrials = 10000; % #of frames in each evaluation
        Eb_N0     = 14;   % Eb/N0, in dB
    case 4
        numTrials = 50000;  % #of frames in each evaluation
        Eb_N0     = 8:2:10;   % Eb/N0, in dB
    case 5
        numTrials = 100000; % #of frames in each evaluation
        Eb_N0     = 12:2:20;   % Eb/N0, in dB
    case 6
        numTrials = 1000000; % #of frames in each evaluation
        Eb_N0     = 22;   % Eb/N0, in dB
    case 7
        numTrials = 1000000; % #of frames in each evaluation
        Eb_N0     = 26;   % Eb/N0, in dB
    case 8
        numTrials = 1000; % #of frames in each evaluation
        Eb_N0     = 8;   % Eb/N0, in dB
    case 9
        numTrials = 10000; % #of frames in each evaluation
        Eb_N0     = 22:2:24;   % Eb/N0, in dB
    case 10
        numTrials = 10000; % #of frames in each evaluation
        Eb_N0     = 26:2:28;   % Eb/N0, in dB
    case 11
        numTrials = 1; % #of frames in each evaluation
        Eb_N0     = 0:2:18;
    otherwise
        numTrials = 100000; % #of frames in each evaluation
        Eb_N0     = 16;
end
SNR = Eb_N0 + 10*log10(Cr*log2(M_mod)) - 10*log10(sf);

% scrambling settings
switch scrambl_profile
    case 1
        ...
    otherwise
        on_off_scrambling = 0; % no scrambling
end

% interleaving settings
switch intlv_profile
    case 1
        on_off_interleaving_chip      = 0; % chip-wise interleaving, 1: enable; 0: disable
        on_off_freq_interleaving_chip = 1; % chip-wise frequency interleaving
    otherwise
        on_off_interleaving_chip      = 0; % chip-wise interleaving, 1: enable; 0: disable
        on_off_freq_interleaving_chip = 0; % chip-wise frequency interleaving
end

% constellations pre-setting
symb_tmp = modulate(modem.qammod(M_mod),(0: M_mod-1));
symb_pow = mean(abs(symb_tmp).^2);
alpha_i = sort(unique(real(symb_tmp)));
alpha_i_size = length(alpha_i);
alpha_i_mat = repmat(alpha_i,nsymb,1);
alpha_q = sort(unique(imag(symb_tmp)));
alpha_q_size = length(alpha_q);
alpha_q_mat = repmat(alpha_q,nsymb,1);
symb_sample = repmat(reshape(modulate(modem.qammod(M_mod),(0:M_mod-1)),1,[]),[nsymb,1]);

% soft-dec curve fitting coefficients
switch M_mod
    case 4
        a_cf = -.3024;
        b_cf = -.9306;
        c_cf = -.04903;
        % var = 10^(a_cf * mse^b_cf + c_cf)
    case 8
        a_cf = -.1555;
        b_cf = -.8582;
        c_cf = -.2447;
        % var = 10^(a_cf * mse^b_cf + c_cf)
    case 16
        a_cf = -.1517;
        b_cf = -.7809;
        c_cf = -.1978;
        % var = 10^(a_cf * mse^b_cf + c_cf)
    case 32
%         a_cf = -.1655;
%         b_cf = -.6745;
%         c_cf = -.2145;
        a_cf = -0.03295;
        b_cf = -0.942;
        c_cf = -0.8841;
        % var = 10^(a_cf * mse^b_cf + c_cf)
    case 64
        a_cf = -0.01537;
        b_cf = -0.951;
        c_cf = -1.206;
        % var = 10^(a_cf * mse^b_cf + c_cf)
    otherwise
        % no def.
end

Wdft = zeros(nfft,nfft); % DFT matrix
for p = 1:nfft
    Wdft(p,:) = exp(-1i*2*pi*(0:nfft-1)*(p-1)/nfft);
end
Wdft = Wdft/sqrt(nfft);

% End of script